//
//  MineViewController.m
//  PersonnelWashCar
//
//  Created by 张越 on 16/4/20.
//  Copyright © 2016年 张越. All rights reserved.
//

#import "MineViewController.h"
#import "FortuneRecordController.h"
#import "MessageViewController.h"
#import "SetUpViewController.h"
#import "UserInfoViewController.h"


@interface MineViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *userImage;
@property (strong, nonatomic) IBOutlet UILabel *userNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *userMobileLabel;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userImage.layer.cornerRadius = self.userImage.width / 2;
    self.userImage.layer.masksToBounds = YES;
    [self webRequest_GetUserInfoMessage];
}


#pragma mark -- 网络请求 --
-(void)webRequest_GetUserInfoMessage
{
    NSString *uidStr = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserIdKey];
    NSDictionary *dict = @{@"uid":uidStr};
    __weak typeof(self)weakSelf = self;
    [HttpRequestManager getMethod:KUserInfoInterface parameters:dict completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        
        if (success) {
            [weakSelf.userImage sd_setImageWithURL:response[@"data"][@"headimgurl"] placeholderImage:[UIImage imageNamed:@"个人头像"]];
            weakSelf.userMobileLabel.text = response[@"data"][@"mobile"];
            weakSelf.userNameLabel.text = response[@"data"][@"nickname"];
            [[NSUserDefaults standardUserDefaults] setObject:response[@"data"][@"nickname"] forKey:KUserDefaultsUserNameKey];
            if ([response[@"data"][@"nickname"] isEqualToString:response[@"data"][@"mobile"]]) {
                weakSelf.userNameLabel.text = @"请填写真实姓名";
                [[NSUserDefaults standardUserDefaults] setObject:@"请填写真实姓名" forKey:KUserDefaultsUserNameKey];
            }
            [[NSUserDefaults standardUserDefaults] setObject:response[@"data"][@"mobile"] forKey:KUserDefaultsUserMobileNO];
            [[NSUserDefaults standardUserDefaults] setObject:response[@"data"][@"headimgurl"] forKey:KUserDefaultsUserPhotoUrl];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }else{
            
        }
    }];
}

// 进入个人资料界面
- (IBAction)intoUserInfoButton:(UIButton *)sender {
    UserInfoViewController *userVC = [[UserInfoViewController alloc]init];
    userVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userVC animated:YES];
    
}

// 进入财富记录界面
- (IBAction)intoFortuneRecordBtn:(UIButton *)sender {
    FortuneRecordController *recordVC = [[FortuneRecordController alloc]init];
    recordVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:recordVC animated:YES];
}

// 进入消息界面
- (IBAction)intoMessageButton:(UIButton *)sender {
    MessageViewController *messageVC = [[MessageViewController alloc]init];
    messageVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messageVC animated:YES];
}

// 进入设置界面
- (IBAction)intoUpSetButton:(UIButton *)sender {
    SetUpViewController *setUpVC = [[SetUpViewController alloc]init];
    setUpVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:setUpVC animated:YES];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *userNameKey = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserNameKey];
    NSString *userMobileKey = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserMobileNO];
    NSString *userPhotoUrl = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserPhotoUrl];
    self.userNameLabel.text = userNameKey;
    self.userMobileLabel.text = userMobileKey;
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:userPhotoUrl] placeholderImage:[UIImage imageNamed:@"个人头像"]];
    
    if (userNameKey == nil || userMobileKey == nil || userPhotoUrl == nil) {
        [self webRequest_GetUserInfoMessage];
    }
    
    self.navigationController.view.backgroundColor = [UIColor clearColor];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = YES; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = [UIColor clearColor]; //bar背景颜色
        [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    }else{
        [self.navigationController.navigationBar setTintColor:[UIColor clearColor]]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"方格"] forBarMetrics:UIBarMetricsDefault];
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                NSArray *list2=imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2=(UIImageView *)obj2;
                        imageView2.hidden=YES;
                    }
                }
            }
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor =[UIColor colorWithRed:0.071 green:0.075 blue:0.078 alpha:1.000]; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }else{
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.071 green:0.075 blue:0.078 alpha:1.000]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
